#coding:utf-8
'''
Created on 2019年2月18日

@author: wps
'''
import json
import requests

from flask import render_template, redirect, request,make_response
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

from app import app
from app.utils.wxhelper import wxhelper


@app.route('/main/test', methods=['POST', 'GET'])
def get_test():
    return 'suc'


@app.route('/action/wx/connect', methods=['POST', 'GET'])
def wx_join():
    if request.method == 'GET':
        signature = request.args.get('signature')
        timestamp = request.args.get('timestamp')
        nonce = request.args.get('nonce')
        echostr = request.args.get('echostr')
        if wxhelper.check_signature(signature,timestamp,nonce):
            return make_response(echostr)
    else:
        xml_data = request.data
        print xml_data
        root = ET.fromstring(xml_data)
        openid = root.find('FromUserName').text
        msgtype = root.find('MsgType').text
        from_user = root.find('ToUserName').text
        print openid
        print msgtype
        if msgtype == 'event':
            event = root.find('Event').text
            content = root.find('EventKey').text
            print event
            if event == 'subscribe':
                return make_response(wxhelper.produce_response_msg(openid,from_user,'欢迎关注！！！[微笑]'))
            elif event == 'CLICK':
                if content == 'WEATHER':
                    url = 'http://t.weather.sojson.com/api/weather/city/101100207'
                    result = requests.get(url)
                    jsd = json.loads(result.text)
                    weather_str = '''【今日天气】
湿       度 : %s
PM 2.5 : %s
空气质量 : %s
注意事项 : %s '''

                    jsd = jsd.get('data')
                    weather = weather_str%(jsd.get('shidu'),jsd.get('pm25'),
                                           jsd.get('quality'),jsd.get('ganmao'))
                    return make_response(wxhelper.produce_response_msg(openid,from_user,weather))
                else:
                    return make_response(wxhelper.produce_response_msg(openid,from_user,'未开发'))
        elif msgtype == 'text':
            content = root.find('Content').text
            return make_response(wxhelper.produce_response_msg(openid,from_user, content))
        elif msgtype == 'voice':
            return make_response(wxhelper.produce_response_msg(openid,from_user,'未开发'))
        elif msgtype == 'image':
            return make_response(wxhelper.produce_response_msg(openid,from_user,'未开发'))
        elif msgtype == 'video':
            return make_response(wxhelper.produce_response_msg(openid,from_user,'未开发'))
        elif msgtype == 'location':
            lacation = root.find('Label').text
            lacation_x = root.find('Location_X').text
            lacation_y = root.find('Location_Y').text
            return make_response(wxhelper.produce_response_msg(openid,from_user,lacation))
        else:
            make_response(wxhelper.produce_response_msg(openid,from_user,'[微笑][微笑][微笑]'))